package com.cykj.web.controller.posbiz.entry.service.impl;

import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.pos.domain.BizAreaCode;
import com.cykj.pos.domain.BizMerchant;
import com.cykj.pos.domain.BizPosErminalRate;
import com.cykj.pos.domain.BizPosMachine;
import com.cykj.pos.domain.dto.BizEntrySnDTO;
import com.cykj.pos.domain.posone.BizAreaCodeDTO;
import com.cykj.pos.mapper.BizMerchantMapper;
import com.cykj.pos.mapper.BizPosMachineMapper;
import com.cykj.web.controller.posbiz.entry.service.IBizMerchantEntryNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.web.controller.posbiz.entry.mapper.BizMerchantEntryNewMapper;
import com.cykj.web.controller.posbiz.entry.domain.BizMerchantEntryNewDTO;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商户入件Service业务层处理
 *
 * @author zhj
 * @date 2021-11-03
 */
@Service
public class BizMerchantEntryNewServiceImpl extends ServiceImpl<BizMerchantEntryNewMapper, BizMerchantEntryNewDTO> implements IBizMerchantEntryNewService {
    @Autowired
    BizMerchantEntryNewMapper bizMerchantEntryNewMapper;
    @Autowired
    BizMerchantMapper bizMerchantMapper;
    @Autowired
    BizPosMachineMapper posMachineMapper;
    @Override
    public List<BizMerchantEntryNewDTO> queryList(BizMerchantEntryNewDTO bizMerchantEntry) {
        //当前时间
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> data= bizMerchantEntry.getParams();
        if (!data.isEmpty()){
            //map获取方式：Object beginTime=data.get("key");
            //获取data的beginTime和endTime
            Object beginTime=data.get("beginTime");
            Object endTime=data.get("endTime");
            //beginTime和endTime转换成string
            String beginTimestr= beginTime.toString();
            String endTimestr= endTime.toString();
            //放入到bizMerchTransactions里
            bizMerchantEntry.setBeginTime(beginTimestr);
            bizMerchantEntry.setEndTime(endTimestr);
        }

        List<BizMerchantEntryNewDTO> bizMerchantEntryNewDTOS=bizMerchantEntryNewMapper.queryList(bizMerchantEntry);
        if (bizMerchantEntry.getMerchIdPros()!=2L){
            for (BizMerchantEntryNewDTO bizMerchantEntryNewDTO:bizMerchantEntryNewDTOS){
                if (StringUtils.isNotBlank(bizMerchantEntryNewDTO.getPhone())){
                    bizMerchantEntryNewDTO.setPhone(bizMerchantEntryNewDTO.getPhone().replaceAll(bizMerchantEntryNewDTO.getPhone().substring(3,7),"****"));
                }
                if (StringUtils.isNotBlank(bizMerchantEntryNewDTO.getCertificateno())){
                    bizMerchantEntryNewDTO.setCertificateno(bizMerchantEntryNewDTO.getCertificateno().replaceAll(bizMerchantEntryNewDTO.getCertificateno().substring(5,14),"********"));
                }
                if (StringUtils.isNotBlank(bizMerchantEntryNewDTO.getBankcardaccount())){
                    if (bizMerchantEntryNewDTO.getBankcardaccount().length()==16){
                        bizMerchantEntryNewDTO.setBankcardaccount(bizMerchantEntryNewDTO.getBankcardaccount().replaceAll(bizMerchantEntryNewDTO.getBankcardaccount().substring(5,14),"********"));
                    }else {
                        bizMerchantEntryNewDTO.setBankcardaccount(bizMerchantEntryNewDTO.getBankcardaccount().replaceAll(bizMerchantEntryNewDTO.getBankcardaccount().substring(5,17),"********"));
                    }
                }
            }
        }
        return bizMerchantEntryNewDTOS;
    }

    @Override
    public List<BizMerchantEntryNewDTO> selUnbindlist(BizMerchantEntryNewDTO bizMerchantEntry) {
        List<BizMerchantEntryNewDTO> bizMerchantEntryNewDTOS=bizMerchantEntryNewMapper.selUnbindlist(bizMerchantEntry);
        return bizMerchantEntryNewDTOS;
    }

    public List<BizMerchantEntryNewDTO> inTieList(BizMerchantEntryNewDTO bizMerchantEntry) {
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        if (sysUserman.getUserId()==1){
            sysUserman.setUserId(2L);
        }
        //当前时间
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Object> data= bizMerchantEntry.getParams();
        if (!data.isEmpty()){
            //map获取方式：Object beginTime=data.get("key");
            //获取data的beginTime和endTime
            Object beginTime=data.get("beginTime");
            Object endTime=data.get("endTime");
            //beginTime和endTime转换成string
            String beginTimestr= beginTime.toString();
            String endTimestr= endTime.toString();
            //放入到bizMerchTransactions里
            bizMerchantEntry.setBeginTime(beginTimestr);
            bizMerchantEntry.setEndTime(endTimestr);
        }
        bizMerchantEntry.setMerchIdPros(sysUserman.getUserId());
        List<BizMerchantEntryNewDTO> bizMerchantEntryNewDTOS=bizMerchantEntryNewMapper.inTieList(bizMerchantEntry);
        if (sysUserman.getUserId()!=2L){
            for (BizMerchantEntryNewDTO bizMerchantEntryNewDTO:bizMerchantEntryNewDTOS){
                if (bizMerchantEntryNewDTO.getPhone()!=null){
                    bizMerchantEntryNewDTO.setPhone(bizMerchantEntryNewDTO.getPhone().replaceAll(bizMerchantEntryNewDTO.getPhone().substring(3,7),"****"));
                }
                if (bizMerchantEntryNewDTO.getCertificateno()!=null){
                    bizMerchantEntryNewDTO.setCertificateno(bizMerchantEntryNewDTO.getCertificateno().replaceAll(bizMerchantEntryNewDTO.getCertificateno().substring(5,14),"********"));
                }
                if (bizMerchantEntryNewDTO.getBankcardaccount()!=null){
                    if (bizMerchantEntryNewDTO.getBankcardaccount().length()==16){
                        bizMerchantEntryNewDTO.setBankcardaccount(bizMerchantEntryNewDTO.getBankcardaccount().replaceAll(bizMerchantEntryNewDTO.getBankcardaccount().substring(5,14),"********"));
                    }else {
                        bizMerchantEntryNewDTO.setBankcardaccount(bizMerchantEntryNewDTO.getBankcardaccount().replaceAll(bizMerchantEntryNewDTO.getBankcardaccount().substring(5,17),"********"));
                    }
                }
            }
        }
        return bizMerchantEntryNewDTOS;
    }

    @Override
    public BizMerchantEntryNewDTO getMerchantEntryId(Long id) {
        SysUser sysUserman = SecurityUtils.getLoginUser().getUser();
        if (sysUserman.getUserId()==1l){
            sysUserman.setUserId(2l);
        }
        BizMerchantEntryNewDTO bizMerchantEntryNewDTO=bizMerchantEntryNewMapper.getMerchantEntryId(id);
        StringBuilder stringCertificatestartdate=new StringBuilder(bizMerchantEntryNewDTO.getCertificatestartdate());
        stringCertificatestartdate.insert(4,"-");
        stringCertificatestartdate.insert(7,"-");
        bizMerchantEntryNewDTO.setCertificatestartdate(stringCertificatestartdate.toString());
        if (!isChinese(bizMerchantEntryNewDTO.getCertificateenddate())){
            StringBuilder stringCertificateenddate=new StringBuilder(bizMerchantEntryNewDTO.getCertificateenddate());
            stringCertificateenddate.insert(4,"-");
            stringCertificateenddate.insert(7,"-");
            bizMerchantEntryNewDTO.setCertificateenddate(stringCertificateenddate.toString());
        }
        if (sysUserman.getUserId()!=2L){
            if (bizMerchantEntryNewDTO.getPhone()!=null){
                bizMerchantEntryNewDTO.setPhone(bizMerchantEntryNewDTO.getPhone().replaceAll(bizMerchantEntryNewDTO.getPhone().substring(3,7),"****"));
            }
            if (bizMerchantEntryNewDTO.getCertificateno()!=null){
                bizMerchantEntryNewDTO.setCertificateno(bizMerchantEntryNewDTO.getCertificateno().replaceAll(bizMerchantEntryNewDTO.getCertificateno().substring(5,14),"********"));
            }
            if (bizMerchantEntryNewDTO.getBankcardaccount()!=null){
                if (bizMerchantEntryNewDTO.getBankcardaccount().length()==16){
                    bizMerchantEntryNewDTO.setBankcardaccount(bizMerchantEntryNewDTO.getBankcardaccount().replaceAll(bizMerchantEntryNewDTO.getBankcardaccount().substring(5,14),"********"));
                }else {
                    bizMerchantEntryNewDTO.setBankcardaccount(bizMerchantEntryNewDTO.getBankcardaccount().replaceAll(bizMerchantEntryNewDTO.getBankcardaccount().substring(5,17),"********"));
                }
            }
        }
        return bizMerchantEntryNewDTO ;
    }

    @Override
    public BizMerchantEntryNewDTO getMerchantEntryselSeesSn(String sn) {
        BizMerchantEntryNewDTO bizMerchantEntryNewDTO=bizMerchantEntryNewMapper.getMerchantEntryselSeesSn(sn);
        return bizMerchantEntryNewDTO ;
    }
    @Override
    public int updateByMerchantEntryId(BizMerchantEntryNewDTO bizMerchantEntry) {
        if (bizMerchantEntry.getIsclicka().equals("a") && !bizMerchantEntry.getIsclicka().equals(null)){
            bizMerchantEntry.setShFAreaName(bizMerchantEntry.getShFAreaName());
        }else {
            bizMerchantEntry.setShFAreaName(bizMerchantEntry.getProvinceno());
        }
        if (bizMerchantEntry.getIsclickb().equals("b") && !bizMerchantEntry.getIsclickb().equals(null)){
            bizMerchantEntry.setSiFAreaName(bizMerchantEntry.getSiFAreaName());
        }else {
            bizMerchantEntry.setSiFAreaName(bizMerchantEntry.getCityno());
        }
        if (bizMerchantEntry.getIsclickc().equals("c") && !bizMerchantEntry.getIsclickc().equals(null)){
            bizMerchantEntry.setQFAreaName(bizMerchantEntry.getQFAreaName());
        }else {
            bizMerchantEntry.setQFAreaName(bizMerchantEntry.getAreano());
        }
        return  bizMerchantEntryNewMapper.updateByMerchantEntryId(bizMerchantEntry);
    }
    @Override
    public List<BizAreaCode> selectsh(String type) {
        return  bizMerchantEntryNewMapper.selectsh(type);
    }
    @Override
    public List<BizAreaCode> selectsi(String item) {
        return  bizMerchantEntryNewMapper.selectsi(item);
    }
    @Override
    public List<BizAreaCode> selectq(String item) {
        return  bizMerchantEntryNewMapper.selectq(item);
    }
    @Override
    public List<BizPosErminalRate> selfeechl() {
        return  posMachineMapper.selectPosErminalRate();
    }
    @Override
    public int removeMerchantEntryIds(Long id) {
        /*查询sn*/
        BizMerchantEntryNewDTO bizMerchantEntryNewDTO= bizMerchantEntryNewMapper.getMerchantEntryId(id);
        /*删除激活*/
        bizMerchantEntryNewMapper.removeActivate(bizMerchantEntryNewDTO.getSn());
        /*修改pos表*/
        BizPosMachine bizPosMachine=new BizPosMachine();
        bizPosMachine.setPosActivateStatus("0");
        bizPosMachine.setPosCode(bizMerchantEntryNewDTO.getSn());
        bizMerchantEntryNewMapper.updateBizPosMachine(bizPosMachine);
        int rows=bizMerchantEntryNewMapper.removeMerchantEntryIds(id);
        if (rows>0){
            bizMerchantMapper.generateAllPosMerchParentRelated();
        }
        return  rows;
    }
    public static boolean isChinese(String str) {
        return str.matches("[\u4e00-\u9fa5]+");
    }

    @Override
    public BizMerchantEntryNewDTO selectEntryAlls(Long id) {
        BizMerchantEntryNewDTO bizMerchantEntryNewDTOS=bizMerchantEntryNewMapper.selectEntryAlls(id);
        return bizMerchantEntryNewDTOS;
    }

    @Override
    public int updateBizMerchantEntry(BizMerchantEntryNewDTO bizMerchantEntry) {
        return bizMerchantEntryNewMapper.updateBizMerchantEntry(bizMerchantEntry);
    }

    @Override
    public int updatePosMachine(BizMerchantEntryNewDTO bizMerchantEntryNewDTOS) {
        int rows=bizMerchantEntryNewMapper.updatePosMachine(bizMerchantEntryNewDTOS);
        return rows;
    }

    @Override
    public int intBizEntrySn(BizMerchantEntryNewDTO bizMerchantEntryNewDTOS) {
        int rows=bizMerchantEntryNewMapper.intBizEntrySn(bizMerchantEntryNewDTOS);
        return rows;
    }

    @Override
    public int intStatusRecords(BizMerchantEntryNewDTO bizMerchantEntryNewDTOS) {
        int rows=bizMerchantEntryNewMapper.intStatusRecords(bizMerchantEntryNewDTOS);
        return rows;
    }
}
